﻿using System;
using System.Collections.Generic;
 
using System.Text;

namespace GGSharp.Structures
{
    public abstract class PubDirContactBase : StuctureBase
    {
        protected int _FmNumber;
        protected byte _FmStatus;
        protected string _firstName;
        protected string _lastName;
        protected string _nickName;
        protected short _birthYear;
        protected string _city;
        protected byte _gender;
        protected byte _ActiveOnly;
        protected string _familyName;
        protected string _familycity;
        protected int _fmstart;
        protected int _nextstart;

        public short BirthYear
        {
            get { return _birthYear; }
            set { _birthYear = value; }
        }

        public string City
        {
            get { return _city; }
            set { _city = value; }
        }

        public string FamilyCity
        {
            get { return _familycity; }
            set { _familycity = value; }
        }

        public string FamilyName
        {
            get { return _familyName; }
            set { _familyName = value; }
        }

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public abstract PubDirContactGender Gender { get; set; }

        public byte GenderRaw
        {
            get { return _gender; }
            set { _gender = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public override int Length
        {
            get { return QueryString.Length + 1; }
        }

        public string NickName
        {
            get { return _nickName; }
            set { _nickName = value; }
        }

        protected string QueryString
        {
            get
            {
                StringBuilder query = new StringBuilder();

                if (_FmNumber != 0)
                    query.Append("FmNumber\0" + _FmNumber + "\0");

                if (_firstName != null)
                    query.Append("firstname\0" + _firstName + "\0");

                if (_lastName != null)
                    query.Append("lastname\0" + _lastName + "\0");

                if (_nickName != null)
                    query.Append("nickname\0" + _nickName + "\0");

                if (_birthYear != 0)
                    query.Append("birtyear\0" + _birthYear + "\0");

                if (_city != null)
                    query.Append("city\0" + _city + "\0");

                if (_gender != 0)
                    query.Append("gender\0" + _gender + "\0");

                if (_ActiveOnly != 0)
                    query.Append("ActiveOnly\0" + _ActiveOnly + "\0");

                if (_familyName != null)
                    query.Append("familyname\0" + _familyName + "\0");

                if (_familycity != null)
                    query.Append("familycity\0" + _familycity + "\0");

                if (_fmstart != 0)
                    query.Append("fmstart\0" + _fmstart + "\0");

                return query.ToString();
            }
        }

        protected void ParseField(string key, string value)
        {
            switch (key)
            {
                case "FmNumber":
                    Int32.TryParse(value, out _FmNumber);
                    break;
                case "FmStatus":
                    Byte.TryParse(value, out _FmStatus);
                    break;
                case "firstname":
                    _firstName = value;
                    break;
                case "lastname":
                    _lastName = value;
                    break;
                case "nickname":
                    _nickName = value;
                    break;
                case "birthyear":
                    Int16.TryParse(value, out _birthYear);
                    break;
                case "city":
                    _city = value;
                    break;
                case "gender":
                    Byte.TryParse(value, out _gender);
                    break;
                case "ActiveOnly":
                    Byte.TryParse(value, out _ActiveOnly);
                    break;
                case "familyname":
                    _familyName = value;
                    break;
                case "familycity":
                    _familycity = value;
                    break;
            }
        }

        public void Read(byte[] bytes, int startIndex)
        {
            int i = startIndex;
            string key;
            string value;

            while (i < bytes.Length && bytes[i] != '\0')
            {
                key = ReadCP1250Text(bytes, i);
                i += key.Length + 1;
                value = ReadCP1250Text(bytes, i);
                i += value.Length + 1;

                ParseField(key, value);
            }

            this.ReadedLength = i - startIndex;
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            /*string key;
            string value;

            this.ReadedLength = 0;

            while (true)
            {
                key = ReadCP1250Text(reader);
                this.ReadedLength += 1 + key.Length;

                if (key == "")
                    break;

                value = ReadCP1250Text(reader);
                this.ReadedLength += 1 + value.Length;

                ParseField(key, value);
            }*/

            throw new NotImplementedException();
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            WriteCP1250Text(QueryString, writer);
        }
    }
}
